<template>
  <p>
    <a-space>
      <train-select v-model:trainCode="selectedTrainCode" width="200px"></train-select>
      <a-button type="primary" @click="handleQuery">查询</a-button>
    </a-space>
  </p>
  <a-table :dataSource="trainSeats"
           :columns="columns"
           :pagination="pagination"
           :sticky="true"
           :scroll="scrollOptions"
           @change="handleTableChange"
           :loading="loading">
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'col'">
        <span v-for="item in SEAT_COL_ARRAY" :key="item.code">
          <span v-if="item.code === record.col && item.type === record.seatType">
            {{ item.desc }}
          </span>
        </span>
      </template>
      <template v-else-if="column.dataIndex === 'seatType'">
        <span v-for="item in SEAT_TYPE_ARRAY" :key="item.code">
          <span v-if="item.code === record.seatType">
            {{ item.desc }}
          </span>
        </span>
      </template>
    </template>
  </a-table>
</template>

<script setup lang="ts" name="train-seat-view">
import {ref, onMounted} from 'vue';
import {notification} from "ant-design-vue";
import { query} from "@/api/base/seat";
import {QueryParams} from "@/api/base/seat/type";
import {SEAT_TYPE_ARRAY} from "@/enums/SeatType.ts";
import {SEAT_COL_ARRAY} from "@/enums/SeatColType.ts"
import TrainSelect from "@/components/train-select.vue";

const selectedTrainCode = ref<any>(undefined);

const trainSeats = ref([]);
// 分页的三个属性名是固定的
const pagination = ref({
  total: 0,
  current: 1,
  pageSize: 15,
  pageSizeOptions:['15','30','60','100,','200']
});

const scrollOptions = {
  scrollToFirstRowOnChange:true,
  y: '820px'
}

let loading = ref(false);
const columns = [
  {
    title: '车次编号',
    dataIndex: 'trainCode',
    key: 'trainCode',
  },
  {
    title: '厢序',
    dataIndex: 'carriageIndex',
    key: 'carriageIndex',
  },
  {
    title: '排号',
    dataIndex: 'row',
    key: 'row',
  },
  {
    title: '列号',
    dataIndex: 'col',
    key: 'col',
  },
  {
    title: '座位类型',
    dataIndex: 'seatType',
    key: 'seatType',
  },
  {
    title: '同车厢座序',
    dataIndex: 'carriageSeatIndex',
    key: 'carriageSeatIndex',
  }
];
const pageSizeOptions = ref<string[]>(['15', '20', '30', '40', '50']);

const handleQuery = async (param: QueryParams) => {
  if (!param) {
    param = {
      pageNum: 1,
      pageSize: pagination.value.pageSize,
      trainCode: ""
    };
  }
  loading.value = true;
  try {
    const result = await query({...param, trainCode: selectedTrainCode.value})
    loading.value = false;
    if (result.success) {
      trainSeats.value = result.data as any;
      // 设置分页控件的值
      pagination.value.current = param.pageNum as number;
      pagination.value.total = result.total;
    } else {
      notification.error({message: "失败", description: result.message});
    }
  } catch (e: any) {
    notification.error({message: "失败", description: e.message});
  }
};

const handleTableChange = (page: any) => {
  // console.log("看看自带的分页参数都有啥：" + JSON.stringify(page));
  pagination.value.pageSize = page.pageSize;
  handleQuery({
    pageNum: page.current,
    pageSize: page.pageSize
  });
};

onMounted(() => {
  handleQuery({
    pageNum: 1,
    pageSize: pagination.value.pageSize
  });
})
</script>
